<?php

namespace app\api\controller;
use app\common\model\StaffOrder as StaffOrderModel;
use think\Db;

class Timeout
{
    //定时清除过期的家教急招
    public function teacher_job_clear()
    {
        $clear_ids = '';
        $list = Db::name('teacher_job')->where([
            'status' => 2
        ])->select();

        foreach ($list as &$row) {
            if($row['give_timesec']=='half_hour'){
                $row['accept_cut'] = $row['createtime']+60*30;
            }elseif ($row['give_timesec']=='a_hour'){
                $row['accept_cut'] = $row['createtime']+60*60;
            }elseif ($row['give_timesec']=='a_day'){
                $row['accept_cut'] = $row['createtime']+60*60*24;
            }elseif ($row['give_timesec']=='three_day'){
                $row['accept_cut'] = $row['createtime']+60*60*24*3;
            }elseif ($row['give_timesec']=='a_week'){
                $row['accept_cut'] = $row['createtime']+60*60*24*7;
            }else{
                $row['accept_cut'] = '';
            }
            if($row['accept_cut']){
                if(time()>$row['accept_cut']){

                    //家长设定的时限到了，老师没接单，家长押金自动退，并及时到帐。
                    $params['order'] = $row;
                    $params['type'] = 'job';
                    \think\Hook::listen('xykeep_order_refund_after', $params);

                    Db::name('teacher_job')->where([
                        'id' => $row['id']
                    ])->update([
                        'refundtime' => time(),
                        'status' => -1
                    ]);

                    \think\Hook::listen('xykeep_job_refund_all', $row['id']);
                    $clear_ids .= $row['id'].',';
                }
            }
        }
        echo '急招过期：'.$clear_ids.';';

        // 过首次试课时间48小时老师和家长都未点击试课完成按钮则自动退回家长押金
        $clear_ids = '';
        $list = Db::name('teacher_job')->where([
            'normal' => 1,
            'status' => 3
        ])->select();
        foreach ($list as &$row) {
            $strTime = $row['learn_date'].' '.$row['learn_time'];
            $strTime = strtotime($strTime);
            if(time() > ($strTime + 48 * 3600)){
                Db::name('teacher_job')->where([
                    'id' => $row['id'],
                ])->update([
                    'status' => 4,
                    'refundtime' => time()
                ]);

                $params['order'] = $row;
                $params['type'] = 'job';
                \think\Hook::listen('xykeep_order_refund_after', $params);

                $unPickStaff = (new StaffOrderModel())->where([
                    'job_id' => $row['id'],
                    'status' => 2
                ])->update([
                    'status' => 3,
                    'updatetime' => time()
                ]);
                $clear_ids .= $row['id'].',';
            }
        }
    }
}